package com.csiec.clock.view.model;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.csiec.clock.activity.MainActivity;
import com.csiec.clock.bean.UserInfo;
import com.csiec.clock.bean.login.ResultCode;
import com.csiec.clock.callback.LoginCallBack;
import com.csiec.clock.databinding.RegisterFragmentBinding;
import com.csiec.clock.model.LoginModel;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

public class RegisterViewModel extends ViewModel {

    private LoginModel model;

    private MutableLiveData<String> userName;
    private MutableLiveData<String> passWord;
    private MutableLiveData<String> checkPassWord;

    private Application application;
    @SuppressLint("StaticFieldLeak")
    private Activity activity;
    private RegisterFragmentBinding binding;
    private final Handler handler = new Handler();


    public void init(Activity activity, RegisterFragmentBinding binding) {
        this.application = activity.getApplication();
        this.activity = activity;
        this.binding = binding;
        model = new LoginModel();
        if (userName == null) userName = new MutableLiveData<>();
        if (passWord == null) passWord = new MutableLiveData<>();
        if (checkPassWord == null) checkPassWord = new MutableLiveData<>();
    }

    public void register() {

        //注册
        if (userName.getValue() != null && passWord.getValue() != null && checkPassWord.getValue() != null) {

            if (passWord.getValue().equals(checkPassWord.getValue())) {
                model.login(userName.getValue(), passWord.getValue(), new LoginCallBack() {
                    @Override
                    public void onSuccess(ResultCode resultCode) {
                        if (resultCode.getStatus() == 200) {
                            handler.post(() -> {
                                showTip(QMUITipDialog.Builder.ICON_TYPE_SUCCESS, "注册成功");
                                userName.setValue(null);
                                passWord.setValue(null);
                                checkPassWord.setValue(null);
                            });
                        } else {
                            handler.post(() -> {
                                showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "注册失败");
                            });
                        }
                    }

                    @Override
                    public void onFailed() {
                        handler.post(() -> {
                            showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "网络错误或其它错误");
                        });
                    }
                });
            } else {
                showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "密码与确认密码不一致");
            }
        } else {
            showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "用户名或密码不能为空");
        }

    }

    //提示悬浮窗
    public void showTip(int type, String msg) {
        QMUITipDialog tipDialog = new QMUITipDialog.Builder(activity)
                .setIconType(type)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        binding.textGologin.postDelayed(tipDialog::dismiss, 2000);
    }

    public MutableLiveData<String> getCheckPassWord() {
        return checkPassWord;
    }

    public MutableLiveData<String> getUserName() {
        return userName;
    }

    public MutableLiveData<String> getPassWord() {
        return passWord;
    }
}